<?php
use yii\helpers\Url;

function str_prefix($str, $n = 1, $char = " ")
{
    for ($x = 0; $x < $n; $x++) {
        $str = $char . $str;
    }
    return $str;
}

$web = Url::base();
?>

<input type="hidden" id="role_id" value="<?= $item == null ? '' : $item['id'] ?>">
<div class="modal-body">
    <div class="form-horizontal">
        <div class="form-group">
            <label class="col-lg-3 col-sm-3 control-label">角色名:</label>
            <div class="col-lg-9">
                <input type="text" class="form-control" id="inputName" value="<?= $item == null ? '' : $item['name'] ?>"/>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label col-lg-3">描述:</label>
            <div class="col-lg-9">
                <textarea rows="2" class="form-control" id="inputDescription"><?= $item == null ? '' : $item['description'] ?></textarea>
            </div>
        </div>
        <div class="form-group">
            <label class="col-lg-3 col-sm-3 control-label">是否启用:</label>
            <div class="col-lg-9">
                <select class="form-control" id="inputIsEnabled">
                    <?php
                    if ($item != null) {
                        if ($item['is_enabled'] == 1) {
                            ?>
                            <option value="1" selected>启用</option>
                            <option value="0">禁用</option>
                            <?php
                        } else {
                            ?>
                            <option value="1">启用</option>
                            <option value="0" selected>禁用</option>
                            <?php
                        }
                    } else {
                        ?>
                        <option value="1" selected>启用</option>
                        <option value="0">禁用</option>
                        <?php
                    }
                    ?>
                </select>
            </div>
        </div>
        <div class="form-group">
            <label class="col-lg-3 col-sm-3 control-label">是否系统角色:</label>
            <div class="col-lg-9">
                <div class="switch">
                    <input type="checkbox" id="inputIsSystem"
                        <?php if ($item != null && $item['is_system'] == 1) {
                            echo 'checked="checked"';
                        } ?>
                    ><span
                        class="btn-clipboard popovers" data-trigger="hover"
                        data-placement="right"
                        data-content=""
                        data-original-title="提示"></span>
                </div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-lg-3 col-sm-3 control-label">是否部门角色:</label>
            <div class="col-lg-9">
                <div class="switch">
                    <input type="checkbox" id="inputIsDepartment"
                        <?php if ($item != null && $item['is_system'] == 1) {
                            echo 'checked="checked"';
                        } ?>
                    ><span
                        class="btn-clipboard popovers" data-trigger="hover"
                        data-placement="right"
                        data-content=""
                        data-original-title="提示"></span>
                </div>
            </div>
        </div>
        <div class="form-group">
            <div class="panel">
                <div class="panel-heading">
                    角色功能点配置
                </div>
                <div class="panel-body">
                    <div class="table-responsive">
                        <table class="table table-bordered table-hover table-condensed">
                            <thead class="nowrap">
                            <tr>
                                <th></th>
                                <th>功能点</th>
                                <th>controller id</th>
                                <th>action id</th>
                            </tr>
                            </thead>
                            <tbody>
                            <?php
                            foreach ($functionPointItems as $functionPointItem) {
                                $itemName = $functionPointItem['name'];
                                if ($functionPointItem['level'] > 0) {
                                    $itemName = '● ' . $itemName;
                                }
                                $itemName = str_prefix($itemName, $functionPointItem['level'], '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;');
                                $isChecked = '';
                                if ($item != null) {
                                    foreach ($item['role_function_point_list'] as $functionPoint) {
                                        if ($functionPointItem['id'] == $functionPoint['function_point_id']) {
                                            $isChecked = 'checked';
                                            break;
                                        }
                                    }
                                }
                                ?>
                                <tr>
                                    <th>
                                        <?php
                                        if ($functionPointItem['id'] != '0') {
                                            ?>
                                            <div class="square-blue">
                                                <div class="checkbox">
                                                    <div class="square-blue form-inline icheck">
                                                        <div class="checkbox popovers">
                                                            <input type="checkbox" name="function_point" value="<?= $functionPointItem['id'] ?>" <?= $isChecked ?>>
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>
                                            <?php
                                        }
                                        ?>
                                    </th>
                                    <td><?= $itemName ?></td>
                                    <td><?= $functionPointItem['controller_id'] ?></td>
                                    <td><?= $functionPointItem['action_id'] ?></td>
                                </tr>
                                <?php
                            }
                            ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script type="application/javascript" src="<?= $web ?>/js/icheck-init.js"></script>
<script type="application/javascript">
    $(function () {
        <?php
        if($item['is_system'] == 1){
        ?>
        $("#inputIsSystem").bootstrapSwitch('state', true);
        <?php
        }else{
        ?>
        $("#inputIsSystem").bootstrapSwitch('state', false);
        <?php
        }
        ?>

        <?php
        if($item['is_department'] == 1){
        ?>
        $("#inputIsDepartment").bootstrapSwitch('state', true);
        <?php
        }else{
        ?>
        $("#inputIsDepartment").bootstrapSwitch('state', false);
        <?php
        }
        ?>
    });

</script>